package cn.edu.sjtu.se.dslab.service.impl;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import cn.edu.sjtu.se.dslab.component.data.DomainEntity;
import cn.edu.sjtu.se.dslab.dao.IDomainEntityDao;
import cn.edu.sjtu.se.dslab.dao.IEntityZoneDao;
import cn.edu.sjtu.se.dslab.dao.IProjectDao;
import cn.edu.sjtu.se.dslab.project.EntityZone;
import cn.edu.sjtu.se.dslab.project.Project;
import cn.edu.sjtu.se.dslab.service.IEntityZoneService;

@Service("entityZoneService")
@Transactional
public class EntityZoneServiceImpl implements IEntityZoneService {

	@Resource(name = "entityZoneDao")
	private IEntityZoneDao entityZoneDao;
	
	@Resource(name = "domainEntityDao")
	private IDomainEntityDao domainEntityDao;
	
	@Resource(name="projectDao")
	private IProjectDao projectDao;
	
	

	@Override
	public boolean addEntityZone(EntityZone entityZone, long projectId) {
		Project project = projectDao.queryById(projectId);
		if (project == null) throw new NullPointerException("Project id not found " + projectId);
		entityZone.setProject(project);
		entityZoneDao.save(entityZone);
		return true;
	}

	@Override
	public EntityZone updateEntityZone(EntityZone entityZone) {
		return entityZoneDao.update(entityZone);
	}

	@Override
	public EntityZone getEntityZoneById(long id) {
		return entityZoneDao.queryById(id);
	}

	@Override
	public EntityZone getEntityZoneByName(String name) {
		return entityZoneDao.queryByName(name);
	}

	@Override
	public List<EntityZone> getEntityZoneList() {
		return entityZoneDao.queryByList();
	}

	@Override
	public boolean deleteEntityZoneById(long entityZoneId) {
		EntityZone entityZone = entityZoneDao.queryById(entityZoneId);
		if (null != entityZone) {
			entityZoneDao.delete(entityZone);
			return true;
		}
		return false;
	}

	@Override
	public List<EntityZone> getEntityZoneByProject(long projectId) {
		return entityZoneDao.queryByProject(projectId);
	}
	
	@Override
	public List<EntityZone> getEntityZoneByCategory(long categoryId) {
		return entityZoneDao.queryByCategory(categoryId);
	}
	
	@Override
	public EntityZone getEntityZoneByProjectAndName(long projectId, String name){
		return entityZoneDao.queryByProjectAndName(projectId, name);
	}

	@Override
	public DomainEntity getDomainEntity(long entityZoneId) {
		EntityZone entityZone = getEntityZoneById(entityZoneId);
		if (entityZone == null) throw new NullPointerException();
		String entityUri = entityZone.getEntityUri();
		if(entityUri == null || entityUri.equals("")) return null;
		DomainEntity domainEntity = domainEntityDao.queryByUri(entityUri);
		return domainEntity;
	}
	
//	@Override
//	public void addEntityToZone(long entityZoneId, List<DomainEntity> entityList){
//
//		EntityZone entityZone = getEntityZoneById(entityZoneId);
//		if (entityZone == null) throw new NullPointerException();
//		String entitys = entityZone.getEntitys();
//		Set<String> entityUriSet = new HashSet<String>();
//		if(entitys != null && !entitys.equals("")){
//			String[] uris = entitys.split("|");
//			for (String s: uris){
//				entityUriSet.add(s);
//			}
//		}
//		for(DomainEntity entity : entityList){
//			entityUriSet.add(entity.getUri());
//		}
//		StringBuilder sb = new StringBuilder();
//		for(String uri : entityUriSet){
//			sb.append(uri).append("|");
//		}
//		entityZone.setEntitys(sb.toString());
//		updateEntityZone(entityZone);
//	}
	
	
	
	

//	@Override
//	public Set<DomainEntity> getEntitySet(long entityZoneId) {
//		EntityZone entityZone = getEntityZoneById(entityZoneId);
//		if (entityZone == null) throw new NullPointerException("Entity Zone can't be found:" + entityZoneId);
//		String entitys = entityZone.getEntitys();
//		if(entitys == null || entitys.equals("")) return null;
//		String[] uris = entitys.split("|");
//		Set<DomainEntity> entitySet = new HashSet<DomainEntity>();
//		for (String uri : uris){
//			DomainEntity domainEntity = domainEntityDao.queryByUri(uri);
//			if (domainEntity != null){
//				entitySet.add(domainEntity);
//			}
//		}
//		return entitySet;
//	}

}

 

















